﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data.Linq;
using GestoreSuperProgetto.Model;

namespace GestoreSuperProgetto
{
    public partial class LoginAccess : Form
    {
        public LoginAccess()
        {
            InitializeComponent();
        }

        internal string Username
        {
            get { return userNameTxt.Text; }
        }

        internal string Password
        {
            get { return passwordTxt.Text; }
        }

        public USER ActiveUser
        {
            get;
            internal set;
        }

        private void okBtn_Click(object sender, EventArgs e)
        {
            var man = (Owner as StartForm).DataManager;           

            bool foundUser = false;

            var us = (from user in man.USERS.Include("PERMISSIONS").Include("SUPERPROJECTS_INSTALLED")
                      where user.USERNAME == this.Username
                      select user).FirstOrDefault();
            if (us != null)
            {
                foundUser = true;
                if (USER.Decrypt(us.PASSWORD) == this.Password)
                {
                    this.ActiveUser = us;
                    DialogResult = DialogResult.OK;
                    Close();
                    return;
                }
            }
            //foreach (USERS user in users)
            //{
            //    if (user.USERNAME == this.Username)
            //    {
            //        foundUser = true;
            //        if (user.PASSWORD == this.Password)
            //        {
            //            this.ActiveUser = user;
            //            DialogResult = DialogResult.OK;
            //            Close();
            //            return;
            //        }
            //    }
            //}
            if (!foundUser)
                MessageBox.Show("Utente non trovato!!","Login error",MessageBoxButtons.OK,MessageBoxIcon.Error);
            else
                MessageBox.Show("Password non valida!!", "Login error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}
